var common = require('../../../../utils/common.js');
var app=getApp();
var baseUrl=app.url;

Page({
    data: {
        page:1,
        carList:{},
        __PUBLIC__:app.__PUBLIC__,
        uploadfile:'',

    },
    onLoad:function(){
        common.bestCurl('GET','/personal/index',res=>{
            if(res.statusCode == 200){
                for(var value in res.data.data){
                    for(var val in res.data.data[value]){
                        var uploadfile = res.data.data[value].uploadfile;
                        if(uploadfile!=''){
                            let extStart = uploadfile.lastIndexOf(".");
                            let ext = uploadfile.substring(extStart, uploadfile.length).toUpperCase();
                            let pic = [
                                        '.JPG',
                                        '.PNG',
                                        '.JPEG',
                                        '.BMP',
                                        '.GIF',
                                        '.WEBP'
                                ]; 
                            if(pic.indexOf(ext)!=-1){
                                res.data.data[value].ext  = 'pic';
                            }else if(ext=='.DOC'||ext=='.DOCX'){
                                res.data.data[value].ext  = 'doc';
                            }else if(ext=='.PDF'){
                                res.data.data[value].ext  = 'pdf';
                            }else{
                                console.log('非法文件')
                            }             
                        }
                    }
                }
                this.setData({carList:res.data.data})
            }
        })
    },
    showFile:function (event) {
        var url = event.currentTarget.dataset.url;
        var fileUrl = app.__UPLOADS__+'/'+url;
        var extStart = url.lastIndexOf(".");
        var ext = url.substring(extStart, url.length).toUpperCase();
        var pic = [
            '.JPG',
            '.PNG',
            '.JPEG',
            '.BMP',
            '.GIF',
            '.WEBP'
        ];
        var fileType = [
            '.DOC',
            '.XLS',
            '.PPT',
            '.PDF',
            '.DOCX',
            '.XLSX',
            '.PPTX'
        ];
        if(pic.indexOf(ext)!=-1){
            wx.previewImage({
                current: '', 
                urls: [
                    fileUrl
                ] 
            })
        }else if(fileType.indexOf(ext)!=-1){
            wx.showLoading({
              title: '加载中',
              mask:true
            });
            wx.downloadFile({
                url: fileUrl, 
                success: function(res) {
                    wx.hideLoading();
                    if (res.statusCode === 200) {
                        wx.openDocument({
                            filePath: res.tempFilePath,
                            success: function (res) {
                                console.log('打开文档成功')
                            }
                        })
                    }
                }
            });
        }else{
            wx.showModal({
                content: '非法文件,禁止预览',
                showCancel: false,
            });        
        }          
    }
})
